package com.queue.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.queue.config.FanoutConfig;
import com.queue.config.NormalConfig;
import com.queue.config.TopicConfig;

/**
* @comments
* @author  zg
* @date 2018年11月10日
*/
@RestController
public class QueueTestController {
	
	@Autowired
    private RabbitTemplate rabbitTemplate;
	
	@RequestMapping("")
	public String test() {
		
		return "Server is running.";
	}
	
	@RequestMapping("normal/send")
	public String normalSend(String msg) {
		
		this.rabbitTemplate.convertAndSend(NormalConfig.QUEUE_NORMAL, msg);
		
		return "ok";
	}
	
	@RequestMapping("topic/send")
	public String topicSend(@RequestParam("rk") String routingKey) {
		String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		String msg =  " topic消息... " + time;
		this.rabbitTemplate.convertAndSend(TopicConfig.topic_exchange, routingKey, msg);
		
		return "ok";
	}
	
	@RequestMapping("fanout/send")
	public String fanoutSend() {
		String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		String msg =  " Hello everyone... " + time;
		this.rabbitTemplate.convertAndSend(FanoutConfig.exchange, "", msg);
		
		return "ok";
	}

}
